package com.hengpeng.itfinoss.service.stm.impl;

import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.persist.QueryResult;
import com.hengpeng.common.persist.entity.AbstractEntity.BoolValue;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.model.Order.OrderType;
import com.hengpeng.common.util.StringHelper;
import com.hengpeng.itfinbase.persist.Comment;
import com.hengpeng.itfinbase.persist.Comment.CommentType;
import com.hengpeng.itfinoss.service.ServiceFactory;
import com.hengpeng.itfinoss.service.stm.CommentService;

/**
 *  留言信息服务接口实现
 * @author lanmingming
 */
@Service
@Lazy
public class CommentServiceImpl implements CommentService
{

	@Autowired
	private ServiceFactory serviceFactory;

	@Override
	public ServiceResult<Comment> delect(Set<Object> ids)
	{
		for (Object id : ids)
		{
			serviceFactory.getPersistService().delete(Comment.class, id);
		}
		return new ServiceResult<Comment>();
	}

	@Override
	public ServiceResult<Comment> get(long id)
	{
		Comment comment = serviceFactory.getPersistService().get(Comment.class, id);
		return new ServiceResult<Comment>(comment);
	}

	@Override
	public ServiceResult<Comment> save(Comment comment)
	{
		serviceFactory.getPersistService().save(comment);
		return new ServiceResult<Comment>();
	}

	public QueryResult<Comment> selectCommentWithPage(CommentType commentType, BoolValue auditState,
			BoolValue replyState, String content, String reply, String title, String commUserName,
			String replyUserName, BoolValue isHidden, int pageIndex, int pageSize,
			LinkedHashMap<String, OrderType> sortMap)
	{
		DynamicQuery pagination = new DynamicQuery(Comment.class);
		pagination.setIsSearchTotalCount(true);
		if (commentType != null)
			pagination.eq("commentType", commentType);
		if (auditState != null)
			pagination.eq("auditState", auditState);
		if (replyState != null)
			pagination.eq("replyState", replyState);
		if (!StringHelper.isEmpty(content))
			pagination.like("content", "%" + content + "%");
		if (!StringHelper.isEmpty(reply))
			pagination.like("reply", "%" + reply + "%");
		if (!StringHelper.isEmpty(title))
			pagination.like("title", "%" + title + "%");
		if (!StringHelper.isEmpty(commUserName))
			pagination.like("commUserName", "%" + commUserName + "%");
		if (!StringHelper.isEmpty(replyUserName))
			pagination.like("replyUserName", "%" + replyUserName + "%");
		if (isHidden != null)
			pagination.eq("isHidden", isHidden);
		pagination.setPageIndex(pageIndex);
		pagination.setPageSize(pageSize);
		if (sortMap != null)
		{
			for (Entry<String, OrderType> entry : sortMap.entrySet())
			{
				if (entry.getValue() == OrderType.DESC)
				{
					pagination.desc(entry.getKey());
				}
				else
				{
					pagination.asc(entry.getKey());
				}
			}
		}
		pagination.getSqlResult().getSql();
		return serviceFactory.getPersistService().findWithPage(Comment.class, pagination);
	}

	@Override
	public ServiceResult<Comment> update(Comment comment)
	{
		Comment obj = serviceFactory.getPersistService().update(comment);
		return new ServiceResult<Comment>(obj);
	}

}
